package com.cl.ks.flow.base;

import com.alibaba.fastjson.JSON;
import com.cl.ks.service.KsFlowGraphRecordService;
import com.cl.spark.dto.SparkResult;
import com.cl.spark.factory.SparkNodeFactory;
import com.cl.spark.util.SparkUtil;
import com.cl.ks.entity.*;
import com.cl.ks.flow.enums.NodeHandlerEnum;
import com.cl.ks.service.KsFlowGraphLogService;
import com.cl.ks.service.KsFlowNodeResultService;
import com.cl.ks.flow.KsFlowGraphHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseFlowNodeHandler {

    @Autowired
    private KsFlowGraphLogService ksFlowGraphLogService;
    @Autowired
    public SparkUtil sparkUtil;
    @Autowired
    private KsFlowNodeResultService ksFlowNodeResultService;
    @Autowired
    private KsFlowGraphRecordService ksFlowGraphRecordService;

    @Autowired
    public SparkNodeFactory sparkNodeFactory;

    protected BaseFlowNodeHandler() {
    }

    public SparkResult handle(ProcessParam processParam) {
        if (KsFlowGraphHandler.isKsFlowGraphRunning(processParam.getKsFlowGraph().getId())) {
            return process(processParam);
        }
        throw new RuntimeException("流程图强制终止:ID " + processParam.getKsFlowGraph().getId());
    }

    public abstract SparkResult process(ProcessParam processParam);

    public abstract NodeHandlerEnum getType();

    public static KsFlowNodeResult newKsFlowNodeResult(
            Integer ksFlowGraphRecordId,
            Integer ksFlowGraphLogId,
            Integer ksFlowGraphId,
            String nodeName,
            String nodeCode,
            String result,
            Integer ksFlowTaskId,
            String createdBy,
            String createdByShowName,
            String power
    ) {
        KsFlowNodeResult ksFlowNodeResult = new KsFlowNodeResult();
        ksFlowNodeResult.setKsFlowGraphRecordId(ksFlowGraphRecordId);
        ksFlowNodeResult.setKsFlowGraphLogId(ksFlowGraphLogId);
        ksFlowNodeResult.setKsFlowGraphId(ksFlowGraphId);
        ksFlowNodeResult.setNodeName(nodeName);
        ksFlowNodeResult.setNodeCode(nodeCode);
        ksFlowNodeResult.setResult(result);
        ksFlowNodeResult.setKsFlowTaskId(ksFlowTaskId);
        ksFlowNodeResult.setCreatedBy(createdBy);
        ksFlowNodeResult.setCreatedByShowName(createdByShowName);
        ksFlowNodeResult.setPower(power);
        return ksFlowNodeResult;
    }


    @Getter
    public static class ProcessParam {
        private final SparkNode sparkNode;
        private final KsFlowGraph ksFlowGraph;
        private final KsFlowGraphRecord ksFlowGraphRecord;
        private final KsFlowGraphLog ksFlowGraphLog;
        private final Boolean testFlag;

        private final List<KsFlowNodeResult> ksFlowNodeResultList = new ArrayList<>();

        public ProcessParam(SparkNode sparkNode, KsFlowGraph ksFlowGraph, KsFlowGraphRecord ksFlowGraphRecord, KsFlowGraphLog ksFlowGraphLog, Boolean testFlag) {
            this.sparkNode = sparkNode;
            this.ksFlowGraph = ksFlowGraph;
            this.ksFlowGraphRecord = ksFlowGraphRecord;
            this.ksFlowGraphLog = ksFlowGraphLog;
            this.testFlag = testFlag;
        }
    }

}
